// Environment configuration
// Safe environment variable access that works in browser
const getEnv = (key: string, defaultValue: string = ''): string => {
  // Check if we're in a browser environment
  if (typeof window !== 'undefined') {
    // Try to get from window if available (for build-time injection)
    const windowEnv = (window as any).ENV;
    if (windowEnv && windowEnv[key]) {
      return windowEnv[key];
    }
  }
  
  // Try import.meta.env (Vite)
  if (typeof import.meta !== 'undefined' && import.meta.env) {
    const viteEnv = import.meta.env[key];
    if (viteEnv !== undefined) {
      return String(viteEnv);
    }
  }
  
  // Fallback to default value
  return defaultValue;
};

export const config = {
  // API Configuration
  apiUrl: getEnv('VITE_API_URL', 'https://fohoytxwqapuleblgefa.supabase.co/functions/v1/make-server-5cc66405'),
  
  // Authentication
  tokenKey: 'db_console_token',
  userKey: 'db_console_user',
  refreshTokenKey: 'db_console_refresh_token',
  
  // Request timeouts (in milliseconds)
  defaultTimeout: 10000,
  authTimeout: 15000,
  
  // Development mode
  isDevelopment: getEnv('MODE', 'development') === 'development',
  
  // Demo mode (for testing without real backend)
  demoMode: getEnv('VITE_DEMO_MODE', 'false') === 'true',
  
  // Demo credentials
  demo: {
    username: 'admin',
    password: 'password'
  }
};

export default config;